import { isAny } from 'bpmn-js/lib/features/modeling/util/ModelingUtil';
import { batchCreateCustom, customShapeAction, customFlowAction } from '../../utils';

export default function ContextPadProvider(
  this: any,
  contextPad: { registerProvider: (arg0: any) => void },
  config: { autoPlace?: any },
  injector: { get: (arg0: string, arg1: boolean) => any },
  translate: any,
  bpmnFactory: any,
  elementFactory: any,
  create: any,
  modeling: any,
  connect: any,
) {
  this.create = create;
  this.elementFactory = elementFactory;
  this.translate = translate;
  this.bpmnFactory = bpmnFactory;
  this.modeling = modeling;
  this.connect = connect;
  config = config || {};
  if (config.autoPlace !== false) {
    this.autoPlace = injector.get('autoPlace', false);
  }
  contextPad.registerProvider(this);
}

ContextPadProvider.$inject = [
  'contextPad',
  'config',
  'injector',
  'translate',
  'bpmnFactory',
  'elementFactory',
  'create',
  'modeling',
  'connect',
];

ContextPadProvider.prototype.getContextPadEntries = function (element: { businessObject: any, type: string }) {
  const { autoPlace, create, elementFactory, translate, modeling, bpmnFactory, connect } = this;

  var actions = {};
  var businessObject = element.businessObject;

  // if(element?.type == 'bpmn:SequenceFlow'){
  //   modeling.setColor(element, {
  //     stroke: '#999'
  //   });
  // }

  // 删除功能
  function removeElement(e: any) {
    modeling.removeElements([element]);
  }

  function startConnect(event: any, element: any) {
    connect.start(event, element);
  }

  function clickElement(e: any) {
    console.log(element);
  }

  function createAction(type: any, group: any, className: any, title: any, options: any) {
    function appendCustomElements(type: any) {
      return function (event: any, element: any) {
        if (autoPlace) {
          const businessObject = bpmnFactory.create(type);
          const shape = elementFactory.createShape(
            Object.assign(
              {
                type,
                businessObject,
              },
              options,
            ),
          );
          autoPlace.append(element, shape);
        } else {
          appendCustomElementsStart(event, element);
        }
      };
    }

    function appendCustomElementsStart(type: any) {
      return function (event: any) {
        const businessObject = bpmnFactory.create(type);
        const shape = elementFactory.createShape(
          Object.assign(
            {
              type,
              businessObject,
            },
            options,
          ),
        );
        create.start(event, shape, element);
      };
    }
    return {
      group,
      className,
      title: translate(title),
      action: {
        click: appendCustomElements(type),
        dragstart: appendCustomElementsStart(type),
      },
    };
  }

  function createConnect(type: any, group: any, className: any, title: any, options: any) {
    return {
      group,
      className,
      title,
      action: {
        click: startConnect,
        dragstart: startConnect,
      },
    };
  }

  function editElement() {
    // 创建编辑图标
    return {
      group: 'edit',
      className: 'bpmn-custom-palette bpmn-custom-palette-edit',
      title: translate('编辑'),
      action: {
        click: clickElement,
      },
    };
  }

  function deleteElement() {
    return {
      group: 'edit',
      className: 'bpmn-custom-palette bpmn-custom-palette-delete',
      title: translate('删除'),
      action: {
        click: removeElement,
      },
    };
  }

  // 只有点击列表中的元素才会产生的元素
  if (
    isAny(businessObject, [
      // 'bpmn:StartEvent',
      'bpmn:Task',
      'bpmn:UserTask',
      'bpmn:InclusiveGateway',
      'bpmn:ParallelGateway',
      'bpmn:ExclusiveGateway',
      'bpmn:CallActivity',
    ])
  ) {
    Object.assign(actions, {
      ...batchCreateCustom(customFlowAction, createConnect), // 连接线
      // ...batchCreateCustom(customShapeAction, createAction),
      // edit: editElement(),
      // delete: deleteElement(),
    });
  }
  // 结束节点和线只有删除和编辑
  // if (isAny(businessObject, ["bpmn:EndEvent", "bpmn:SequenceFlow", "bpmn:DataOutputAssociation"])) {
  //     Object.assign(actions, {
  //         edit: editElement(),
  //         delete: deleteElement(),
  //     });
  // }
  return actions;
};
